/****************************************************************
 * Fulguro
 * Copyright (C) 2004 Christophe Clienti
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 ***************************************************************/


#include <flgrCoreData.h>



#define tbit fgBIT
#define set(x)  ((fgBIT) (x));
#define shiftr(a,x) ((a)>>(x))
#define shiftl(a,x) ((a)<<(x))
#define and(a,b) ((a) & (b))
#define or(a,b) ((a) | (b))
#define empty() 



void flgr1d_shift_1_right_spp1_fgBIT(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, fgBIT insert) {
  tbit *pdest = (tbit*) datdest->array;
  tbit *psrc = (tbit*) datsrc->array;
  tbit r0;
  tbit r1;
  tbit r2;
  tbit rmask;
  int i;
  int nbvec = datdest->length / (sizeof(tbit)*8);

  r2 = set((insert != 0) ? ((fgBIT) 1)<<(sizeof(tbit)*8-1) : 0);
  rmask = set(1);

  for(i=0 ; i<nbvec+1; i++, psrc++, pdest++) {
    r0 = *psrc;
    r1 = shiftr(r0,1);
    r1 = or(r2,r1);
    r2 = and(r0,rmask);
    r2 = shiftl(r2,(sizeof(tbit)*8-1));
    *pdest = r1;    
  }
  empty();
}

void flgr1d_shift_1_left_spp1_fgBIT(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, fgBIT insert) {
  tbit *pdest = (tbit*) datdest->array;
  tbit *psrc = (tbit*) datsrc->array;
  tbit r0;
  tbit r1;
  tbit r2;
  tbit rmask;
  int i;
  int nbvec = datdest->length / (sizeof(tbit)*8);
  int posvec = (sizeof(tbit)*8)-(datdest->length % (sizeof(tbit)*8));

  rmask = set(((fgBIT) 1)<<(sizeof(tbit)*8-1));
  
  r2 = set((insert != 0) ? ((fgBIT) 1)<<posvec : 0);

  psrc += nbvec;
  pdest += nbvec;

  for(i=nbvec+1 ; i>=0 ; i--, psrc--, pdest--) {
    r0 = *psrc;
    r1 = shiftl(r0,1);
    r1 = or(r2,r1);
    r2 = and(r0,rmask);
    r2 = shiftr(r2,(sizeof(tbit)*8-1));
    *pdest = r1;    
  }
  empty();
}
